package com.wlit;

import com.wlit.UHF18.UHFReader;
import com.wlit.handler.NettyWebSocketHandler;
import com.wlit.server.NettyWebServer;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.StringJoiner;
import java.util.Timer;
import java.util.TimerTask;

public class MainFrame extends JFrame implements IExtOps{

    private Logger logger = Logger.getLogger(MainFrame.class);
    private static final long serialVersionUID = 1L;
    private UHFReader uhf = new UHFReader();
    String[] s = {"COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9", "COM10", "COM11", "COM12"};
    String[] bauds = {"9600bps", "19200bps", "38400bps", "57600bps", "115200bps"};
    String[] powers = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14",
            "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26"};
    private final JLabel label_4 = new JLabel();
    private int fCmdRet = 0x30;
    private final JPanel panel_2 = new JPanel();
    private final JLabel label_3 = new JLabel();
    private final JComboBox comboBox = new JComboBox();
    private final JButton button = new JButton();
    private final JLabel label_6 = new JLabel();
    private final JDesktopPane desktopPane_1 = new JDesktopPane();
    private final JLabel label_1 = new JLabel();
    private final JComboBox comboBox_port = new JComboBox(s);
    private final JLabel label_5 = new JLabel();
    private final JComboBox comboBox_baud = new JComboBox(bauds);
    private final JButton bt_open = new JButton();
    private final JButton bt_close = new JButton();
    private final JLabel label_7 = new JLabel();
    private final JComboBox comboBox_power = new JComboBox(powers);
    private final JButton bt_power = new JButton();
    private final JComboBox comboBox_epc = new JComboBox();
    private final JRadioButton rb_psd = new JRadioButton();
    private final JRadioButton rb_epc = new JRadioButton();
    private final JRadioButton rb_tid = new JRadioButton();
    private final JRadioButton rb_user = new JRadioButton();
    private final ButtonGroup group = new ButtonGroup();
    private final ButtonGroup group1 = new ButtonGroup();
    private final ButtonGroup group2 = new ButtonGroup();
    private final JLabel addrLabel = new JLabel();
    private final JTextField text_num = new JTextField();
    private final JLabel label_8 = new JLabel();
    private final JTextField text_addr = new JTextField();
    private final JLabel label_9 = new JLabel();
    private final JTextField text_psd = new JTextField();
    private final JLabel label_10 = new JLabel();
    private final JTextField text_data = new JTextField();
    private final JButton bt_write_epc = new JButton();
    private final JButton bt_read = new JButton();
    private final JButton bt_write = new JButton();
    private final JButton bt_start = new JButton();
    private final JButton bt_stop = new JButton();
    private final JLabel label = new JLabel();
    private final JLabel label_2 = new JLabel();
    private final JTextField text_tagnum = new JTextField();
    private final DefaultListModel listmodel = new DefaultListModel();
    private final JList list = new JList(listmodel);
    private final JLabel ipLabel = new JLabel();
    private final JTextField text_ip = new JTextField();
    private final JLabel portLabel = new JLabel();
    private final JTextField text_port = new JTextField();
    private final JButton bt_con = new JButton();
    private final JButton bt_discon = new JButton();
    private final JRadioButton rb_tcp = new JRadioButton();
    private final JPanel panel = new JPanel();
    private final JRadioButton rb_com = new JRadioButton();
    private final JPanel panel_1 = new JPanel();
    private final JRadioButton rb_band1 = new JRadioButton();
    private final JRadioButton rb_band2 = new JRadioButton();
    private final JRadioButton rb_band3 = new JRadioButton();
    private final JRadioButton rb_band4 = new JRadioButton();
    private final JButton bt_Region = new JButton();

    private NettyWebServer nettyWebServer = new NettyWebServer(this);

    /**
     * Launch the application.
     */
    public static void main(String[] args) {

        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

                    JFrame.setDefaultLookAndFeelDecorated(true);

                    MainFrame frame = new MainFrame();
                    frame.setVisible(true);
                    //启动服务器
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            frame.startNettyServer();
                        }
                    },"RFIDServer").start();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

    }

    public void startNettyServer(){
        nettyWebServer.run();
    }
    /**
     * Create the
     */
    public MainFrame() {
        setTitle("为乐电子标签读写器(适用DS302/309/310/312)V1.0");
        getContentPane().setLayout(null);
        setResizable(false);
        setBounds(100, 100, 654, 488);
        //setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
//        addWindowListener(new WindowAdapter() {
//            @Override
//            public void windowClosing(WindowEvent e) {
//                super.windowClosing(e);
//                nettyWebServer.close();
//            }
//        });
        try {
            this.setIconImage(ImageIO.read(getClass().getResourceAsStream("/icon.jpeg")));
//            String icon = MainFrame.class.getResource("/icon.jpeg").getFile();
//            this.setIconImage(new ImageIcon(icon).getImage());
        }catch (Exception ex){ ex.printStackTrace();}

        setToolSystemTray();

        getContentPane().add(desktopPane_1);
        desktopPane_1.setLayout(null);
        desktopPane_1.setBounds(153, 305, 287, -228);

//        comboBox_epc.setBounds(492, 152, 226, 30);
//        getContentPane().add(comboBox_epc);
        label_1.setBounds(23, 15, 40, 30);
        getContentPane().add(label_1);
        label_1.setText("串口:");
        comboBox_port.setBounds(70, 15, 86, 30);
        getContentPane().add(comboBox_port);
        comboBox_port.setSelectedIndex(0);
        label_5.setBounds(175, 15, 60, 30);
        getContentPane().add(label_5);
        label_5.setText("波特率：");
        comboBox_baud.setBounds(238, 15, 100, 30);
        getContentPane().add(comboBox_baud);
        comboBox_baud.setSelectedIndex(3);
        bt_open.setBounds(352, 15, 88, 30);
        bt_open.addActionListener(new Bt_openActionListener());
        getContentPane().add(bt_open);
        bt_open.setText("打 开");
        bt_close.setBounds(446, 15, 88, 30);
        bt_close.addActionListener(new Bt_closeActionListener());

        getContentPane().add(bt_close);
        bt_close.setText("关 闭");

        label_7.setBounds(20, 110, 40, 30);
        getContentPane().add(label_7);
        label_7.setText("功率:");

        comboBox_power.setBounds(64, 110, 72, 30);
        getContentPane().add(comboBox_power);
        comboBox_power.setSelectedIndex(26);

        bt_power.setBounds(145, 110, 86, 30);
        bt_power.addActionListener(new Bt_powerActionListener());
        getContentPane().add(bt_power);
        bt_power.setText("功率设置");

//        addrLabel.setBounds(395, 226, 120, 30);
//        getContentPane().add(addrLabel);
//        addrLabel.setText("起始地址:");
//
//        text_num.setBounds(493, 276, 225, 30);
//        getContentPane().add(text_num);
//        text_num.setText("04");
//
//        label_8.setBounds(395, 276, 120, 30);
//        getContentPane().add(label_8);
//        label_8.setText("字数:");
//
//        text_addr.setBounds(493, 226, 225, 30);
//        getContentPane().add(text_addr);
//        text_addr.setText("00");
//
//        label_9.setBounds(395, 319, 72, 30);
//        getContentPane().add(label_9);
//        label_9.setText("访问密码:");
//
//        text_psd.setBounds(494, 319, 224, 30);
//        getContentPane().add(text_psd);
//        text_psd.setText("00000000");
//
        label_10.setBounds(395, 371, 160, 30);
        getContentPane().add(label_10);
        label_10.setText("状态: 未连接");

//        text_data.setBounds(493, 371, 225, 30);
//        getContentPane().add(text_data);
//        text_data.setText("12345678");

//        bt_read.setBounds(492, 409, 106, 30);
//        bt_read.addActionListener(new Bt_readActionListener());
//        getContentPane().add(bt_read);
//        bt_read.setText("读数据");
//
//        bt_write.setBounds(604, 409, 114, 30);
//        bt_write.addActionListener(new Bt_writeActionListener());
//        getContentPane().add(bt_write);
//        bt_write.setText("写数据");
//
//        bt_write_epc.setBounds(380, 409, 106, 30);
//        bt_write_epc.addActionListener(new Bt_write_epcActionListener());
//        getContentPane().add(bt_write_epc);
//        bt_write_epc.setText("写EPC数据");

        bt_start.setBounds(172, 409, 78, 30);
        bt_start.addActionListener(new Bt_startActionListener());
        getContentPane().add(bt_start);
        bt_start.setText("开始");

        bt_stop.setBounds(270, 409, 78, 30);
        bt_stop.addActionListener(new Bt_stopActionListener());
        getContentPane().add(bt_stop);
        bt_stop.setText("停止");

//        rb_epc.setBounds(470, 190, 78, 30);
//        getContentPane().add(rb_epc);
//        rb_epc.setText("EPC区");
//
//        rb_tid.setBounds(553, 191, 78, 30);
//        getContentPane().add(rb_tid);
//        rb_tid.setText("TID区");
//
//        rb_user.setBounds(632, 191, 86, 30);
//        getContentPane().add(rb_user);
//        rb_user.setText("用户区");
//
//        rb_psd.setBounds(390, 190, 78, 30);
//        getContentPane().add(rb_psd);
//        rb_psd.setText("保留区");
//
//        getContentPane().add(label);
//        label.setText("EPC号:");
//        label.setBounds(395, 164, 66, 18);

        final GridBagConstraints gridBagConstraints_4 = new GridBagConstraints();
        gridBagConstraints_4.gridx = 4;
        gridBagConstraints_4.gridy = 0;

        label_4.setText("  ");

        panel_2.setLayout(new GridBagLayout());

        final GridBagConstraints gridBagConstraints_8 = new GridBagConstraints();
        gridBagConstraints_8.fill = GridBagConstraints.HORIZONTAL;
        gridBagConstraints_8.insets = new Insets(0, 0, -10, 0);
        gridBagConstraints_8.ipady = 20;
        panel_2.add(label_3, gridBagConstraints_8);
        label_3.setText("读写器功率:");

        panel_2.add(comboBox, new GridBagConstraints());

        final GridBagConstraints gridBagConstraints_9 = new GridBagConstraints();
        gridBagConstraints_9.gridx = 4;

        final GridBagConstraints gridBagConstraints_10 = new GridBagConstraints();
        gridBagConstraints_10.gridy = 0;
        gridBagConstraints_10.gridx = 2;
        panel_2.add(label_6, gridBagConstraints_10);
        label_6.setText("    ");

        gridBagConstraints_9.fill = GridBagConstraints.VERTICAL;
        panel_2.add(button, gridBagConstraints_9);
        button.setText("设置");

        bt_discon.setEnabled(false);
        bt_discon.addActionListener(new Bt_disconActionListener());
        bt_con.setEnabled(false);
        bt_con.addActionListener(new Bt_conActionListener());
        bt_open.setEnabled(true);
        bt_close.setEnabled(false);//关闭
        bt_power.setEnabled(false);//功率
        bt_start.setEnabled(false);//开始
        bt_stop.setEnabled(false);//停止
        bt_read.setEnabled(false);//读
        bt_write.setEnabled(false);//写
        bt_write_epc.setEnabled(false);//写
        bt_Region.setEnabled(true);
        bt_Region.addActionListener(new Bt_RegionActionListener());
        rb_user.setSelected(true);
        group.add(rb_psd);
        group.add(rb_epc);
        group.add(rb_tid);
        group.add(rb_user);

        group1.add(rb_com);
        rb_com.setSelected(true);
        rb_com.addActionListener(new Rb_comActionListener());
        getContentPane().add(label_2);
        label_2.setText("张数：");
        label_2.setBounds(37, 415, 40, 18);

        getContentPane().add(text_tagnum);
        text_tagnum.setText("0");
        text_tagnum.setBounds(80, 413, 78, 22);

        getContentPane().add(list);
        list.setLayoutOrientation(JList.HORIZONTAL_WRAP);
        list.setVisibleRowCount(100);
        list.setBounds(20, 157, 328, 234);

        getContentPane().add(panel);
        panel.setBounds(564, 15, 154, 36);

//        panel.add(rb_com);
//        rb_com.setText("COM");
//        group1.add(rb_tcp);
//        rb_tcp.addActionListener(new Rb_tcpActionListener());

//        getContentPane().add(text_ip);
//        text_ip.setText("192.168.1.190");
//        text_ip.setBounds(49, 52, 106, 30);
//
//        getContentPane().add(ipLabel);
//        ipLabel.setText("IP");
//        ipLabel.setBounds(23, 64, 62, 18);
//
//        getContentPane().add(portLabel);
//        portLabel.setText("端口");
//        portLabel.setBounds(175, 64, 62, 18);
//
//        getContentPane().add(text_port);
//        text_port.setText("6000");
//        text_port.setBounds(235, 52, 106, 30);
//
//        getContentPane().add(bt_con);
//        bt_con.setText("打 开");
//        bt_con.setBounds(352, 52, 88, 30);
//
//        getContentPane().add(bt_discon);
//        bt_discon.setText("关 闭");
//        bt_discon.setBounds(446, 52, 88, 30);

//        getContentPane().add(panel_1);
//        final FlowLayout flowLayout = new FlowLayout();
//        flowLayout.setAlignment(FlowLayout.LEFT);
//        panel_1.setLayout(flowLayout);
//        panel_1.setBounds(235, 85, 299, 65);
//
//        panel_1.add(rb_band1);
//        rb_band1.setText("Chinese band2");
//
//        panel_1.add(rb_band2);
//        rb_band2.setText("User band");
//
//        panel_1.add(rb_band3);
//        rb_band3.setText("Korean band");
//
//        panel_1.add(rb_band4);
//        rb_band4.setText("EU band");
//
//        panel.add(rb_tcp);
//        rb_tcp.setText("TCP");
//
//        getContentPane().add(bt_Region);
//        bt_Region.setText("频段设置");
//        bt_Region.setBounds(545, 110, 86, 30);
//        group2.add(rb_band1);
//        group2.add(rb_band2);
//        group2.add(rb_band3);
//        group2.add(rb_band4);
//        rb_band1.setSelected(true);
        getContentPane().add(bt_Region);
        bt_Region.setText("查看有效串口");
        bt_Region.setBounds(390, 110, 140, 30);
    }

    @Override
    public void onClear() {
        listmodel.clear();
        comboBox_epc.removeAllItems();
    }

    private class Bt_openActionListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            bt_open_actionPerformed(e);
        }
    }

    private class Bt_closeActionListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            bt_close_actionPerformed(e);
        }
    }

    private class Bt_powerActionListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            bt_power_actionPerformed(e);
        }
    }

    private class Rb_tcpActionListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            rb_tcp_actionPerformed(e);
        }
    }

    private class Bt_startActionListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            bt_start_actionPerformed(e);
        }
    }

    private class Bt_stopActionListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            bt_stop_actionPerformed(e);
        }
    }

    private class Bt_write_epcActionListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            bt_write_epc_actionPerformed(e);
        }
    }

    private class Bt_readActionListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            bt_read_actionPerformed(e);
        }
    }

    private class Bt_writeActionListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            bt_write_actionPerformed(e);
        }
    }

    private class Rb_comActionListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            rb_com_actionPerformed(e);
        }
    }

    private class Bt_conActionListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            bt_con_actionPerformed(e);
        }
    }

    private class Bt_disconActionListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            bt_discon_actionPerformed(e);
        }
    }

    private class Bt_RegionActionListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            //bt_Region_actionPerformed(e);
            bt_check_avail_buttons(e);
        }
    }

    protected void rb_tcp_actionPerformed(ActionEvent e) {
        if ((uhf.FrmHandle[0] > 0) && (uhf.FrmHandle[0] < 256))
            bt_close_actionPerformed(e);
        bt_discon.setEnabled(false);
        bt_con.setEnabled(true);
        bt_open.setEnabled(false);
        bt_close.setEnabled(false);//关闭

    }

    //打开串口
    protected void bt_open_actionPerformed(ActionEvent e) {
        int ComPort = comboBox_port.getSelectedIndex() + 1;
        byte ComAddr = (byte) 255;
        byte baudRate = (byte) comboBox_baud.getSelectedIndex();
        if (baudRate > 2) baudRate = (byte) (baudRate + 2);
        fCmdRet = uhf.OpenByCom(ComPort, baudRate);
        if (fCmdRet == 0) {
            bt_close.setEnabled(true);//关闭
            bt_open.setEnabled(false);//打开
            bt_power.setEnabled(true);//功率
            bt_start.setEnabled(true);//开始
            bt_stop.setEnabled(true);//停止
            bt_read.setEnabled(true);//读
            bt_write.setEnabled(true);//写
            bt_Region.setEnabled(false);
            bt_write_epc.setEnabled(true);
            label_10.setText("状态：串口已连接");
            JOptionPane.showMessageDialog(null,"串口"+s[comboBox_port.getSelectedIndex()]+"连接成功","提示",JOptionPane.INFORMATION_MESSAGE);
        }else{
            label_10.setText("状态：串口未连接");
            JOptionPane.showMessageDialog(null,"串口"+s[comboBox_port.getSelectedIndex()]+"连接失败","提示",JOptionPane.ERROR_MESSAGE);
        }
    }

    //关闭串口
    protected void bt_close_actionPerformed(ActionEvent e) {
        fCmdRet = uhf.CloseByCom();
        if (fCmdRet == 0) {
            bt_close.setEnabled(false);//关闭
            bt_open.setEnabled(true);//打开
            bt_power.setEnabled(false);//功率
            bt_start.setEnabled(false);//开始
            bt_stop.setEnabled(false);//停止
            bt_read.setEnabled(false);//读
            bt_write.setEnabled(false);//写
            bt_Region.setEnabled(true);
            bt_write_epc.setEnabled(false);
            listmodel.clear();
            comboBox_epc.removeAllItems();
            timeQyery = false;
        }
    }

    //设置功率
    protected void bt_power_actionPerformed(ActionEvent e) {
        int power = comboBox_power.getSelectedIndex();
        fCmdRet = uhf.SetPower(power);
        if (fCmdRet != 0) {
            JOptionPane.showMessageDialog(null, "功率设置失败", "提示", JOptionPane.ERROR_MESSAGE);
        } else {
            JOptionPane.showMessageDialog(null, "功率设置成功", "提示", JOptionPane.INFORMATION_MESSAGE);
        }
    }

    public void timeVoid() {
        final Timer timer = new Timer();
        TimerTask tt = new TimerTask() {
            @Override
            public void run() {
                if (timeQyery) {
                    if (Qyeryflag) return;
                    Qyeryflag = true;
                    Inventory();
                    Qyeryflag = false;
                    timer.cancel();
                    timeVoid();
                }
            }
        };
        timer.schedule(tt, 50);
    }

    public byte[] stringToByte(String str) {
        byte[] b = new byte[str.length() / 2];
        for (int i = 0; i < str.length() / 2; i++) {
            b[i] = (byte) (0xff & Integer.parseInt(str.substring(i * 2, i * 2 + 2), 16));
        }
        return b;
    }

    private void Inventory() {
        String[] EPC;
        EPC = uhf.Inventory();
        if (EPC != null) {
            for (int m = 0; m < EPC.length; m++) {
                String orgin = EPC[m];
                //后台进行编码，直接发送给前端
                byte[] buf = stringToByte(orgin);
                String sepc = new String(buf, StandardCharsets.ISO_8859_1).trim();
                logger.info("读取的标签:"+orgin+"::解码后标签:"+sepc);


                boolean inlist = false;
                for (int n = 0; n < comboBox_epc.getItemCount(); n++) {
                    String temp = comboBox_epc.getItemAt(n).toString();
                    if (temp.equals(sepc)) {
                        inlist = true;
                        break;
                    }
                }
                if (inlist == false) {
                    listmodel.addElement(sepc);
                    comboBox_epc.addItem(sepc);
                    //这里真正的发送电子标签
                    NettyWebSocketHandler.getClients().writeAndFlush(new TextWebSocketFrame(sepc));
                }
            }
            comboBox_epc.setSelectedIndex(0);
            text_tagnum.setText(String.valueOf(comboBox_epc.getItemCount()));
        }

    }

    private boolean timeQyery = false;//定时器执行标志
    private boolean Qyeryflag = false;//单次询查结束标志

    protected void bt_start_actionPerformed(ActionEvent e) {
        timeQyery = true;
        listmodel.clear();
        comboBox_epc.removeAllItems();
        label_10.setText("状态：开始");
        bt_stop.setEnabled(true);
        bt_start.setEnabled(false);
        timeVoid();
    }

    protected void bt_stop_actionPerformed(ActionEvent e) {
        timeQyery = false;
        label_10.setText("状态：已停止");
        bt_stop.setEnabled(false);
        bt_start.setEnabled(true);
    }

    protected void bt_write_epc_actionPerformed(ActionEvent e) {
        String Psd = text_psd.getText();
        String epcString = text_data.getText();
        fCmdRet = uhf.WriteEPC(epcString, Psd);
        if (fCmdRet != 0) {
            JOptionPane.showMessageDialog(null, "写数据失败", "提示", JOptionPane.ERROR_MESSAGE);
        } else {
            JOptionPane.showMessageDialog(null, "写数据成功", "提示", JOptionPane.INFORMATION_MESSAGE);
        }
    }

    protected void bt_read_actionPerformed(ActionEvent e) {
        String EPC = comboBox_epc.getSelectedItem().toString();
        byte WordPtr = (byte) (0xff & Integer.parseInt(text_addr.getText(), 16));
        byte Num = (byte) (0xff & Integer.parseInt(text_num.getText(), 16));
        byte Mem = 0;
        if (rb_psd.isSelected()) {
            Mem = 0;
        }
        if (rb_epc.isSelected()) {
            Mem = 1;
        }
        if (rb_tid.isSelected()) {
            Mem = 2;
        }
        if (rb_user.isSelected()) {
            Mem = 3;
        }
        byte[] Psd = new byte[4];
        Psd = uhf.stringToByte(text_psd.getText());
        String result = uhf.ReadData(EPC, WordPtr, Num, Mem, Psd);
        text_data.setText(result);
    }

    protected void bt_write_actionPerformed(ActionEvent e) {
        String EPC = comboBox_epc.getSelectedItem().toString();
        byte WordPtr = (byte) (0xff & Integer.parseInt(text_addr.getText(), 16));
        byte Mem = 0;
        if (rb_psd.isSelected()) {
            Mem = 0;
        }
        if (rb_epc.isSelected()) {
            Mem = 1;
        }
        if (rb_tid.isSelected()) {
            Mem = 2;
        }
        if (rb_user.isSelected()) {
            Mem = 3;
        }
        byte[] Psd = new byte[4];
        Psd = uhf.stringToByte(text_psd.getText());
        String str_data = text_data.getText();
        byte Num = (byte) (str_data.length() / 4);
        byte[] Data;
        Data = uhf.stringToByte(str_data);
        fCmdRet = uhf.WriteData(EPC, WordPtr, Num, Data, Mem, Psd);
        if (fCmdRet != 0) {
            JOptionPane.showMessageDialog(null, "写数据失败", "提示", JOptionPane.ERROR_MESSAGE);
        } else {
            JOptionPane.showMessageDialog(null, "写数据成功", "提示", JOptionPane.INFORMATION_MESSAGE);
        }
    }

    protected void rb_com_actionPerformed(ActionEvent e) {
        bt_discon_actionPerformed(e);
        bt_discon.setEnabled(false);
        bt_con.setEnabled(false);
        bt_open.setEnabled(true);
        bt_close.setEnabled(false);//关闭

    }

    protected void bt_con_actionPerformed(ActionEvent e) {
        String ipaddr = "";
        int Port = 0;
        ipaddr = text_ip.getText().toString();
        Port = Integer.valueOf(text_port.getText());
        fCmdRet = uhf.OpenByTcp(ipaddr, Port);
        if (fCmdRet == 0) {
            bt_discon.setEnabled(true);//关闭
            bt_con.setEnabled(false);//打开
            bt_power.setEnabled(true);//功率
            bt_start.setEnabled(true);//开始
            bt_stop.setEnabled(true);//停止
            bt_read.setEnabled(true);//读
            bt_write.setEnabled(true);//写
            bt_Region.setEnabled(true);
            bt_write_epc.setEnabled(true);
        }
    }

    protected void bt_discon_actionPerformed(ActionEvent e) {

        fCmdRet = uhf.CloseByTcp();
        if (fCmdRet == 0) {
            bt_discon.setEnabled(false);//关闭
            bt_con.setEnabled(true);//打开
            bt_power.setEnabled(false);//功率
            bt_start.setEnabled(false);//开始
            bt_stop.setEnabled(false);//停止
            bt_read.setEnabled(false);//读
            bt_write.setEnabled(false);//写
            bt_Region.setEnabled(false);
            bt_write_epc.setEnabled(false);
            timeQyery = false;
            listmodel.clear();
            comboBox_epc.removeAllItems();
        }
    }

    protected void bt_Region_actionPerformed(ActionEvent e) {

        int band = 2;
        if (rb_band1.isSelected()) {
            band = 1;
        }
        if (rb_band2.isSelected()) {
            band = 2;
        }
        if (rb_band3.isSelected()) {
            band = 3;
        }
        if (rb_band4.isSelected()) {
            band = 4;
        }
        fCmdRet = uhf.SetRegion(band);
        if (fCmdRet == 0) {
            JOptionPane.showMessageDialog(null, "设置成功", "提示", JOptionPane.INFORMATION_MESSAGE);
        } else {
            JOptionPane.showMessageDialog(null, "设置失败", "提示", JOptionPane.ERROR_MESSAGE);
        }
    }

    protected void bt_check_avail_buttons(ActionEvent e){
        StringJoiner sj = new StringJoiner(",");
        for (int i = 0; i < s.length; i++) {
            int ComPort = i + 1;//端口号
            byte ComAddr = (byte) 255;
            //波特率为5--57600bps--出厂默认值
            byte baudRate = 5;
            fCmdRet = uhf.OpenByCom(ComPort, baudRate);
            if (fCmdRet == 0) {
               sj.add(s[i]);
            }
            fCmdRet = uhf.CloseByCom();
        }
        JOptionPane.showMessageDialog(null, "检查到有效串口"+sj.toString(), "提示", JOptionPane.INFORMATION_MESSAGE);
    }


    /* 实现系统托盘化 */
    protected void setToolSystemTray() {
        // 系统是否支持系统托盘
        if (SystemTray.isSupported()) {

            // 获取SystemTray系统托盘实例
            SystemTray tray = SystemTray.getSystemTray();

            // 弹出式 菜单
            PopupMenu popup = new PopupMenu();

            // 创建关于菜单项
            MenuItem aboutItem = new MenuItem("打开窗口");
            aboutItem.addActionListener(new ActionListener(){

                @Override
                public void actionPerformed(ActionEvent e) {
                    setVisible(true);
                }

            });
            popup.add(aboutItem);

            // 创建退出菜单项
            MenuItem exitItem = new MenuItem("退出");
            exitItem.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    nettyWebServer.close();
                    System.exit(0);
                }
            });
            popup.add(exitItem);
            // 构造托盘图标
//            String icon = MainFrame.class.getResource("/icon.jpeg").getFile();
//            ImageIcon imageIcon = new ImageIcon(icon);
            Image image = null;
            try{
                image = ImageIO.read(getClass().getResourceAsStream("/icon.jpeg"));
            }catch (Exception ex){
                String icon = MainFrame.class.getResource("/icon.jpeg").getFile();
                ImageIcon imageIcon = new ImageIcon(icon);
                image = imageIcon.getImage();
            }
            TrayIcon trayIcon = new TrayIcon(image, "为乐电子标签读写器", popup);

            trayIcon.setImageAutoSize(true);
            // 鼠标事件
            trayIcon.addMouseListener(new MouseAdapter() {

                public void mouseClicked(MouseEvent e) {
                    // 单击显示窗口
                    if (e.getClickCount() == 1) {
                        setVisible(true);
                    }
                }
            });

            // 添加托盘图标
            try {
                tray.add(trayIcon);
            } catch (AWTException e1) {
                e1.printStackTrace();
            }
        } else {
            JOptionPane.showMessageDialog(null,
                    System.getProperty("os.version") + "系统不支持系统托盘!");
        }
    }

}
